/*
 * Copyright (C) 2006-2007 Eskil Bylund
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

using System;
using System.Collections.Generic;

using Gtk;

namespace DCSharp.Gui
{
	public static class IconManager
	{
		private static Widget widget;
		private static Dictionary<string, Gdk.Pixbuf> iconCache;

		private static Gdk.Pixbuf userOnline;
		private static Gdk.Pixbuf userOffline;
		private static Gdk.Pixbuf userPassive;
		private static Gdk.Pixbuf userOP;

		private static Gdk.Pixbuf hubOnline;
		private static Gdk.Pixbuf hubOffline;

		static IconManager()
		{
			widget = new Invisible();
			iconCache = new Dictionary<string, Gdk.Pixbuf>();
		}

		#region Properties

		public static Gdk.Pixbuf UserOnline
		{
			get { return userOnline; }
		}

		public static Gdk.Pixbuf UserOffline
		{
			get { return userOffline; }
		}

		public static Gdk.Pixbuf UserPassive
		{
			get { return userPassive; }
		}

		public static Gdk.Pixbuf UserOP
		{
			get { return userOP; }
		}

		public static Gdk.Pixbuf HubOnline
		{
			get { return hubOnline; }
		}

		public static Gdk.Pixbuf HubOffline
		{
			get { return hubOffline; }
		}

		#endregion

		#region Methods

		public static void Init()
		{
			//Window.DefaultIcon = Gdk.Pixbuf.LoadFromResource("Logo.png");
			Window.DefaultIconName = "gtk-network";

			// Load icons
			string[] icons = {"user-online", "user-offline", "user-passive"};
			foreach (string icon in icons)
			{
				IconTheme.AddBuiltinIcon(icon, 16,
					Gdk.Pixbuf.LoadFromResource(Util.GetResource(icon + ".png")));
			}

			// Update
			UpdateIcons();
			IconTheme.Default.Changed += OnIconThemeChanged;
		}

		public static Gdk.Pixbuf GetIcon(string iconName, int size)
		{
			// The icons are cached because a new pixbuf might be created every
			// time an icon is loaded.
			string iconId = iconName + size;
			if (iconCache.ContainsKey(iconId))
			{
				return iconCache[iconId];
			}
			else
			{
				Gdk.Pixbuf pixbuf = null;
				if (IconTheme.Default.HasIcon(iconName))
				{
					 pixbuf = IconTheme.Default.LoadIcon(iconName, size,
					 	IconLookupFlags.UseBuiltin);
				}
				iconCache.Add(iconId, pixbuf);
				return pixbuf;
			}
		}

		private static void UpdateIcons()
		{
			userOnline = GetIcon("user-online", 16);
			userOffline = GetIcon("user-offline", 16);
			userPassive = GetIcon("user-passive", 16);
			userOP = widget.RenderIcon(Stock.Help, IconSize.Menu, String.Empty);

			hubOnline = GetIcon("network-server", 16);
			if (hubOnline == null)
			{
				hubOnline = GetIcon(Stock.Network, 16);
			}

			// NOTE: network-server-offline is not in the Icon Naming Specification.
			hubOffline = IconManager.GetIcon("network-server-offline", 16);
			if (hubOffline == null)
			{
				hubOffline = new Gdk.Pixbuf(Gdk.Colorspace.Rgb, true, 8,
					hubOnline.Width, hubOnline.Height);

				hubOnline.SaturateAndPixelate(hubOffline, 1.0F, true);
			}
		}

		private static void OnIconThemeChanged(object obj, EventArgs args)
		{
			iconCache.Clear();
			UpdateIcons();
		}

		#endregion
	}
}
